package com.isoft.illustrationsdraw.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Comparator;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import com.isoft.illustrationsdraw.Constants;
import com.isoft.illustrationsdraw.entity.IllustrationCollection;
import com.isoft.illustrationsdraw.service.FileService;
import com.isoft.illustrationsdraw.service.IllustrationCollectionService;
import com.isoft.illustrationsdraw.service.ResourceService;
import com.isoft.illustrationsdraw.service.UserService;

@Service
public class ResourceServiceImpl implements ResourceService{

    private final FileService fileService;
    private final UserService userService;
    private final IllustrationCollectionService illustrationCollectionService;
  
  
    public ResourceServiceImpl(FileService fileService, UserService userService, IllustrationCollectionService illustrationCollectionService) {
        this.fileService = fileService;
        this.userService = userService;
        this.illustrationCollectionService = illustrationCollectionService;
    }
    
    @Override
    public List<Resource> getIllustrationResourceById(Long id) throws IllegalArgumentException, IOException {
        try {
            List<String> paths = getIllustrationPathsById(id);
            List<Resource> resources = new ArrayList<>();
            for (String path : paths) {
                resources.add(getResourceByPath(path));
            }
            return resources;
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Resource getSingleIllustrationResourceById(Long id, Integer index) throws IllegalArgumentException, IOException {
        try {
            String path = Constants.File.ILLUSTRATIONS_DIR + id.toString() + "/" + index.toString();
            return getResourceByPath(path);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public Resource getProfileResourceByUserId(Long userId) throws IllegalArgumentException, IOException {
        try {
            String path = userService.getById(userId).getProfileImg();
            return getResourceByPath(path);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<String> getIllustrationPathsById(Long id) throws IllegalArgumentException {
        var illustrationCollection = illustrationCollectionService.getCollectionByIllustrationId(id);
        if (illustrationCollection == null) {
            throw new IllegalArgumentException(Constants.Error.INVALID_ILLUSTRATION_ID);
        }
        
        return illustrationCollection.stream()
            .sorted(Comparator.comparingInt(IllustrationCollection::getOrderIndex)) // 使用 IllustrationCollection 的 getOrderIndex 方法
            .map(IllustrationCollection::getImageUrl) // 提取 imageUrl
            .collect(Collectors.toList()); // 收集到列表
    }

    @Override
    public Resource getResourceByPath(String path) throws IllegalArgumentException, IOException {
        try {
            return new FileSystemResource(fileService.getFileByPath(path));
        } catch (Exception e) {
            throw e;
        }
    }
}
